package com.yzk.auction.controller;

import com.yzk.auction.entity.Bidding;
import com.yzk.auction.entity.GoodsDTO;
import com.yzk.auction.service.BiddingService;
import com.yzk.auction.service.GoodsService;
import com.yzk.auction.utils.PageModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "homePage",urlPatterns = "/homePage")
public class HomePageController extends HttpServlet {
    private GoodsService goodsService = new GoodsService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    String second_name;
    String like;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String method = request.getParameter("method");
        if (method.equals("homepage_list")) {
            this.homePageList(request, response);
        } else if (method.equals("comm_datils")) {
            this.commDatils(request,response);
        } else if(method.equals("homePage_like")){   //根据分类id查询商品，分页显示
            this.homePage_like(request,response);
        } else if(method.equals("homePage_id")){   //根据分类id查询商品，分页显示
            this.homepage_id(request,response);
        }

    }
    private void homePage_like(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String p = request.getParameter("p");
        String r = request.getParameter("r");
        System.out.println(p);
        if(p==null) {
            like = request.getParameter("like");
        }
        //GoodsDao goodsDao = new GoodsDao();
        if(p == null) {
            p = "1";
        }
        if(r == null) {
            r = "8";
        }
        try {
            PageModel paginationlike = goodsService.paginationlike(like,Integer.parseInt(p),Integer.parseInt(r));
            request.setAttribute("pageModel1", paginationlike);
            request.getRequestDispatcher("WEB-INF/jsp/selectpage.jsp").forward(request, response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    protected void homePageList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String p = request.getParameter("p");
        String r = request.getParameter("r");
        if (p == null) {
            p = "1";
        }
        if (r == null) {
            r = "3";
        }
        PageModel pageModel = goodsService.pagination(Integer.parseInt(p), Integer.parseInt(r));
        System.out.println(pageModel);
        request.setAttribute("pageModelMenu", pageModel);
        request.getRequestDispatcher("WEB-INF/jsp/homepage_list.jsp").forward(request, response);
    }

    protected void commDatils(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String goods_id = request.getParameter("goods_id");
        List<GoodsDTO> list =GoodsService.listGoods();
        int id = Integer.parseInt(goods_id);
        GoodsDTO goodsDTO = null;
        for (GoodsDTO goods:list
        ) {
            if(id==goods.getGoods_id()){
                goodsDTO=goods;
            }
        }
        List<Bidding> bid_list = BiddingService.listGoods(id);
        request.setAttribute("goods",goodsDTO);
        request.setAttribute("bidding",bid_list);
        request.getRequestDispatcher("comm_datils.jsp").forward(request,response);
    }
    /**
     * //根据分类id查询商品，分页显示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void homepage_id(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String p = request.getParameter("p");
        String r = request.getParameter("r");
        if (p==null) {
            second_name = request.getParameter("second_name");
        }
        System.out.println(second_name);

        if(p == null) {
            p = "1";
        }
        if(r == null) {
            r = "8";
        }

        //根据二级分类名查询
        try {
            PageModel pageModel1 = goodsService.paginationsecondid(second_name,Integer.parseInt(p),Integer.parseInt(r));
            request.setAttribute("pageModel1", pageModel1);
            request.getRequestDispatcher("WEB-INF/jsp/sortpage.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
